package com.example.tse.rbyair;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tse.rbyair.downloadresponsibility.CheckUpdateHandler;
import com.example.tse.rbyair.downloadresponsibility.DownApkHandler;
import com.example.tse.rbyair.login.LoginActivity;
import com.example.tse.rbyair.util.FragmentAdapter;
import com.example.tse.rbyair.util.SharedPreferenceUtils;


public class MainActivity extends FragmentActivity {
    private RelativeLayout radio_button1;
    private RelativeLayout radio_button2;
    private RelativeLayout radio_button3;
    private RelativeLayout radio_button4;
    private RelativeLayout radio_button5;
    public ImageView iv1,iv2,iv3,iv4,iv5;
    private TextView tv1,tv2,tv3,tv4,tv5;
    //	private static ImageView shopcart_point;
    private static ImageView my_point;
    private static TextView shopcart_num;
    private RelativeLayout shoppingcart;
    private FrameLayout frameLayout;

    private FragmentAdapter fragmentAdapter;
    private Fragment fragment ;
    private int currentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initViews();

        currentIndex = getIntent().getIntExtra("index", 0);

        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(),getApplicationContext());
        fragment = (Fragment) fragmentAdapter.instantiateItem(frameLayout, 2);
        fragmentAdapter.setPrimaryItem(frameLayout, 2, fragment);
        fragmentAdapter.finishUpdate(frameLayout);


        radio_button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = 0;
                clearColor();
                clearDrawable();
                iv1.setImageResource(R.drawable.tabbar_select_pin);
                tv1.setTextColor(getResources().getColor(R.color.pink_font));
                currentIndex = 0;

                fragment = (Fragment) fragmentAdapter.instantiateItem(frameLayout, currentIndex);
                fragmentAdapter.setPrimaryItem(frameLayout, currentIndex, fragment);
                fragmentAdapter.finishUpdate(frameLayout);

            }
        });

        radio_button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = 1;
                clearColor();
                clearDrawable();
                iv2.setImageResource(R.drawable.tabbar_select_activity);
                tv2.setTextColor(getResources().getColor(R.color.pink_font));
                currentIndex = 1;
                if(SharedPreferenceUtils.getValueByKey(MainActivity.this, "memberId").equals("")){
                    Toast.makeText(MainActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);

                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
//                            turn2FirstPage();
                        }
                    }, 1000);
                    return;
                }

                fragment = (Fragment) fragmentAdapter.instantiateItem(frameLayout, currentIndex);
                fragmentAdapter.setPrimaryItem(frameLayout, currentIndex, fragment);
                fragmentAdapter.finishUpdate(frameLayout);
            }
        });
        radio_button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = 2;
                clearColor();
                clearDrawable();
                iv3.setImageResource(R.drawable.tabbar_select_home);
                tv3.setTextColor(getResources().getColor(R.color.pink_font));

                fragment = (Fragment) fragmentAdapter.instantiateItem(frameLayout, currentIndex);
                fragmentAdapter.setPrimaryItem(frameLayout, currentIndex, fragment);
                fragmentAdapter.finishUpdate(frameLayout);
            }
        });
        radio_button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = 3;
                clearColor();
                clearDrawable();
                iv4.setImageResource(R.drawable.tabbar_select_shoppingcart);
                tv4.setTextColor(getResources().getColor(R.color.pink_font));
                if(SharedPreferenceUtils.getValueByKey(MainActivity.this, "memberId").equals("")){
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
//                            turn2FirstPage();
                        }
                    }, 1000);
                    return;
                }
                fragment = (Fragment) fragmentAdapter.instantiateItem(frameLayout, currentIndex);
                fragmentAdapter.setPrimaryItem(frameLayout, currentIndex, fragment);
                fragmentAdapter.finishUpdate(frameLayout);
            }
        });
        radio_button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = 4;
                clearColor();
                clearDrawable();
                iv5.setImageResource(R.drawable.tabbar_select_my);
                tv5.setTextColor(getResources().getColor(R.color.pink_font));
                fragment = (Fragment) fragmentAdapter.instantiateItem(frameLayout, currentIndex);
                fragmentAdapter.setPrimaryItem(frameLayout, currentIndex, fragment);
                fragmentAdapter.finishUpdate(frameLayout);
            }
        });

/**
 *
 *
 */
        CheckUpdateHandler checkUpdateHandler = new CheckUpdateHandler();
        DownApkHandler downApkHandler = new DownApkHandler();
        checkUpdateHandler.setSuccessor(downApkHandler);
        checkUpdateHandler.handlerRequest(this);

//        accountLoginRequest();

    }

    private void initViews(){
        //		shopcart_point = (ImageView)findViewById(R.id.shopcart_point);
//        my_point = (ImageView)findViewById(R.id.my_point);
//        shopcart_num = (TextView)findViewById(R.id.shopcart_num);
        frameLayout = (FrameLayout)findViewById(R.id.layout_content);
        radio_button1 = (RelativeLayout)findViewById(R.id.radio_button1);
        radio_button2 = (RelativeLayout)findViewById(R.id.radio_button2);
        radio_button3 = (RelativeLayout)findViewById(R.id.radio_button3);
        radio_button4 = (RelativeLayout)findViewById(R.id.radio_button4);
        radio_button5 = (RelativeLayout)findViewById(R.id.radio_button5);

        iv1 = (ImageView)findViewById(R.id.iv1);
        iv2 = (ImageView)findViewById(R.id.iv2);
        iv3 = (ImageView)findViewById(R.id.iv3);
        iv4 = (ImageView)findViewById(R.id.iv4);
        iv5 = (ImageView)findViewById(R.id.iv5);

        tv1 = (TextView)findViewById(R.id.tv1);
        tv2 = (TextView)findViewById(R.id.tv2);
        tv3 = (TextView)findViewById(R.id.tv3);
        tv4 = (TextView)findViewById(R.id.tv4);
        tv5 = (TextView)findViewById(R.id.tv5);

    }

    private void clearColor() {
        tv1.setTextColor(getResources().getColor(R.color.gry_font));
        tv2.setTextColor(getResources().getColor(R.color.gry_font));
        tv3.setTextColor(getResources().getColor(R.color.gry_font));
        tv4.setTextColor(getResources().getColor(R.color.gry_font));
        tv5.setTextColor(getResources().getColor(R.color.gry_font));
    }

    private void clearDrawable() {
        iv1.setImageResource(R.drawable.tabbar_notselect_pin);
        iv2.setImageResource(R.drawable.tabbar_notselect_activity);
        iv3.setImageResource(R.drawable.tabbar_notselect_home);
        iv4.setImageResource(R.drawable.tabbar_notselect_shoppingcart);
        iv5.setImageResource(R.drawable.tabbar_notselect_my);
    }


    private static final int TIME_EXIT = 2000;
    private long mExit = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long time = System.currentTimeMillis();
            if (time - mExit > TIME_EXIT) {
                mExit = time;
                Toast.makeText(this, R.string.doubleclickexit, Toast.LENGTH_SHORT).show();
            } else {
                MainActivity.this.finish();
            }
        }
        return false;
    }


    public void goMe() {
        radio_button1.performClick();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }

}

